package hos.kotlin

/**
 * <p>Title: ListExt </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @date : 2022/7/4 11:30
 * @version : 1.0
 */


fun <T> List<T>?.split(split: String = ","): String {
    if (this.isNullOrEmpty()) {
        return ""
    }
    val builder = StringBuilder()
    val size = size
    for (i in 0 until size) {
        if (i > 0) {
            builder.append(split)
        }
        builder.append(get(i))
    }
    return builder.toString()
}

fun <T> Array<T>?.split(split: String = ","): String {
    if (this.isNullOrEmpty()) {
        return ""
    }
    val builder = StringBuilder()
    val size = size
    for (i in 0 until size) {
        if (i > 0) {
            builder.append(split)
        }
        builder.append(get(i))
    }
    return builder.toString()
}


//<editor-fold desc="toList">
/**
 * 转换为Integer列表<br></br>
 *
 * @param split 分隔符
 * @param split 被转换的值
 * @return 结果
 */
fun String?.toIntList(split: String): MutableList<Int> {
    val list = mutableListOf<Int>()
    if (this.isEmptyAny()) {
        return list
    }
    val splits = this!!.split(split.toRegex()).dropLastWhile { it.isEmpty() }
        .toTypedArray()
    for (ss in splits) {
        list.add(ss.toInt())
    }
    return list
}

/**
 * 將字字符串分割成Long類型的列表
 *
 * @param str
 * @return
 */
fun String?.toLongList(split: String): MutableList<Long> {
    val list = mutableListOf<Long>()
    if (this.isEmptyAny()) {
        return list
    }
    val splits = this!!.split(split.toRegex()).dropLastWhile { it.isEmpty() }
        .toTypedArray()
    for (s in splits) {
        list.add(s.toLong())
    }
    return list
}

fun String?.toDoubleList(split: String): MutableList<Double> {
    val list = mutableListOf<Double>()
    if (this.isEmptyAny()) {
        return list
    }
    val splits = this!!.split(split.toRegex()).dropLastWhile { it.isEmpty() }
        .toTypedArray()
    for (ss in splits) {
        list.add(ss.toDouble())
    }
    return list
}

fun String?.toFloatList(split: String): MutableList<Float> {
    val list = mutableListOf<Float>()
    if (this.isEmptyAny()) {
        return list
    }
    val splits = this!!.split(split.toRegex()).dropLastWhile { it.isEmpty() }
        .toTypedArray()
    for (ss in splits) {
        list.add(ss.toFloat())
    }
    return list
}

fun String?.toStringList(split: String): MutableList<String> {
    val list = mutableListOf<String>()
    if (this.isEmptyAny()) {
        return list
    }
    val splits = this!!.split(split.toRegex()).dropLastWhile { it.isEmpty() }
        .toTypedArray()
    for (s in splits) {
        list.add(s)
    }
    return list
}

fun String?.toBooleanList(split: String): MutableList<Boolean> {
    val list = mutableListOf<Boolean>()
    if (this.isEmptyAny()) {
        return list
    }
    val splits = this!!.split(split.toRegex()).dropLastWhile { it.isEmpty() }
        .toTypedArray()
    for (s in splits) {
        list.add(s.toBoolean())
    }
    return list
}
//</editor-fold>


fun <V> List<V>?.getIfNull(index: Int, defaultValue: V?): V? {
    return getIfNull(index) {
        defaultValue
    }
}

fun <V> List<V>?.getIfNull(index: Int, value: () -> V?): V? {
    if (this == null || this.isEmpty() || this.size <= index) {
        return value.invoke()
    }
    return when (val v = this[index]) {
        null -> {
            value.invoke()
        }
        "null", "NULL", "Null" -> {
            value.invoke()
        }
        else -> {
            v
        }
    }
}

fun <V> List<V>?.toNull(index: Int): V? = if (isEmptyAny()) null else getIfNull(index, null)

fun <V> List<V>?.toNullString(index: Int): String? {
    val v: V = toNull(index) ?: return null
    return v.toNullString()
}

fun <V> List<V>?.empty(
    index: Int, value: String = "", start: String = "", end: String = ""
): String {
    val target: String = toNullString<V>(index)
        ?: return start + value + end
    return start + target + end
}

fun <V> List<V>?.divide(index: Int): String {
    return empty<V>(index, "-")
}

fun <V> List<V>?.rate(index: Int, value: String = "-"): String {
    return empty<V>(index, end = "%", value = value)
}

fun <V> List<V>?.like(index: Int): String {
    val like = toNullString(index)
    return if (like == null || like.isEmpty()) {
        "%%"
    } else "%$like%"
}

//<editor-fold desc="Number">
fun <V> List<V>?.asInteger(index: Int, value: Int = 0): Int {
    return toNull(index).asInteger(value)
}

fun <V> List<V>?.toNullInteger(index: Int): Int? {
    return toNull(index).toNullInteger()
}

fun <V> List<V>?.asIntegerString(index: Int, value: Int = 0): String {
    return this.toNull<V>(index).asIntegerString(value)
}

fun <V> List<V>?.toNullIntegerString(index: Int): String? = toNull(index).toNullIntegerString()


fun <V> List<V>?.asDouble(index: Int, value: Double = 0.0): Double =
    toNull<V>(index).asDouble(value)

fun <V> List<V>?.toNullDouble(index: Int): Double? = toNull<V>(index).toNullDouble()

fun <V> List<V>?.asDoubleString(index: Int, value: Double = 0.0): String =
    this.toNull<V>(index).asDoubleString(value)

fun <V> List<V>?.toNullDoubleString(index: Int): String? =
    this.toNull<V>(index).toNullDoubleString()

fun <V> List<V>?.asLong(index: Int, value: Long = 0L): Long = toNull<V>(index).asLong(value)

fun <V> List<V>?.toNullLong(index: Int): Long? = toNull<V>(index).toNullLong()

fun <V> List<V>?.asLongString(index: Int, value: Long = 0L): String =
    this.toNull<V>(index).asLongString(value)

fun <V> List<V>?.toNullLongString(index: Int): String? =
    this.toNull<V>(index).toNullLongString()

fun <V> List<V>?.asFloat(index: Int, value: Float = 0f): Float = toNull<V>(index).asFloat(value)

fun <V> List<V>?.toNullFloat(index: Int): Float? = toNull<V>(index).toNullFloat()

fun <V> List<V>?.asFloatString(index: Int, value: Float = 0f): String =
    this.toNull<V>(index).asFloatString(value)

fun <V> List<V>?.toNullFloatString(index: Int): String? =
    this.toNull<V>(index).toNullFloatString()

fun <V> List<V>?.asBoolean(index: Int, value: Boolean = false): Boolean =
    toNull<V>(index).asBoolean(value)

fun <V> List<V>?.toNullBoolean(index: Int): Boolean? = toNull<V>(index).toNullBoolean()

fun <V> List<V>?.asBooleanString(index: Int, value: Boolean = false): String =
    this.toNull<V>(index).asBooleanString(value)

fun <V> List<V>?.toNullBooleanString(index: Int): String? =
    this.toNull<V>(index).toNullBooleanString()
//</editor-fold>